package game.console;

import game.AbstractGame;
import game.util.Logger;

import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class GameConsole extends AbstractConsole
{
    private boolean CONSOLE_OPEN = false;
    private AbstractGame parent;
    
    public GameConsole(AbstractGame game)
    {
        this.parent = game;
        setXY(0, 0);
    }
    
    public boolean consoleKeyPressed(KeyEvent e)
    {
        return e.getKeyCode() == KeyEvent.VK_F1;
    }
    
    public boolean isOpen()
    {
        return CONSOLE_OPEN;
    }
    
    public void openConsole()
    {
        CONSOLE_OPEN = true;
    }
    
    public void closeConsole()
    {
        clearLineBuffer();
        CONSOLE_OPEN = false;
    }
    
    @Override
    protected Dimension getSize()
    {
        Dimension size = new Dimension(parent.getFrame().getSize());
        size.height /= 3;
        return size;
    }
    
 // ingame console
    
    private KeyAdapter ingameConsoleKeyAdapter;
    
    public KeyAdapter getIngameConsoleKeyAdapter()
    {
        return ingameConsoleKeyAdapter;
    }
    
    public void enableIngameConsole()
    {
        if (ingameConsoleKeyAdapter == null)
        {
            ingameConsoleKeyAdapter = new KeyAdapter() 
            {
                private boolean IGNORE_KEYBOARD = false;
                
                @Override
                public void keyPressed(KeyEvent e)
                {
                    if (IGNORE_KEYBOARD)
                    {
                        e.consume();
                    }
                    else
                    {
                        IGNORE_KEYBOARD = true;
                        if (consoleKeyPressed(e))
                        {
                            if (isOpen())
                            {
                                closeConsole();
                            }
                            else
                            {
                                openConsole();
                            }
                            Logger.trace("CONSOLE OPEN: ", isOpen());
                        }
                        if (isOpen())
                        {
                            fireKeyEvent(e);
                        }
                        IGNORE_KEYBOARD = false;
                    }
                }
            };
            parent.getGameListener().enableGreedyConsole(this);
        }
    }
}
